package veterinaria.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import veterinaria.excepcion.DAOExcepcion;
import veterinaria.modelo.Tarea;
import veterinaria.util.ConexionBD;

public class TareaDAO extends BaseDAO {
	
	public Collection<Tarea> buscarPorNombre(String descripcion)	throws DAOExcepcion {
		String query = "select idTarea, dsTarea, dsComplejidad from tarea where dsTarea like ?";
		Collection<Tarea> lista = new ArrayList<Tarea>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try
		{
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, "%" + descripcion + "%");
			rs = stmt.executeQuery();
			while (rs.next()) {
				Tarea vo = new Tarea();
				vo.setIdTarea(rs.getInt("idTarea"));
				vo.setDsTarea(rs.getString("dsTarea"));
				vo.setDsComplejidad(rs.getString("dsComplejidad"));
				lista.add(vo);
			}
		}
		catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		}
		finally
		{
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		//System.out.println(lista.size());
		return lista;
		}

		public void insertar(Tarea vo) throws DAOExcepcion {
		System.out.println("TareaDAO: insertar(Tarea vo)");
		String query = "insert into Tarea(Cita_idCita, dsTarea, dsComplejidad) values (?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		//ResultSet rs = null;
		try	{
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, vo.getCita_idCita());
			stmt.setString(2, vo.getDsTarea());
			stmt.setString(3,vo.getDsComplejidad());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}		
			// Obtener el ultimo id
			//int id = 0;
			//query = "select last_insert_id()";
			//stmt = con.prepareStatement(query);
			//rs = stmt.executeQuery();
			//if (rs.next()) {
			//	id = rs.getInt(1);
			//}
			//vo.setIdTarea(id);
		}
		catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		}
		finally
		{
			//this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		//return vo;
		}

		public Tarea obtener(int idTarea) throws DAOExcepcion {
		Tarea vo = new Tarea();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select idTarea, Cita_idCita, dsTarea, dsComplejidad from tarea where idTarea=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idTarea);
			rs = stmt.executeQuery();
			if (rs.next()) {
				vo.setIdTarea(rs.getInt(1));				
				vo.setCita_idCita(rs.getInt(2));
				vo.setDsTarea(rs.getString(3));
				vo.setDsComplejidad(rs.getString(4));
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
		}

		public void eliminar(int idTarea) throws DAOExcepcion {
		String query = "delete from Tarea WHERE idTarea=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idTarea);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		}

		public Tarea actualizar(Tarea vo) throws DAOExcepcion {
		String query = "update Tarea set Cita_idCita=?, dsTarea=?, dsComplejidad=? where idTarea=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, vo.getCita_idCita());			
			stmt.setString(2, vo.getDsTarea());
			stmt.setString(3, vo.getDsComplejidad());
			stmt.setInt(4, vo.getIdTarea());
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
		}

		public Collection<Tarea> listar() throws DAOExcepcion {
		Collection<Tarea> c = new ArrayList<Tarea>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "select idTarea,dsTarea,dsComplejidad from Tarea order by dsTarea";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Tarea vo = new Tarea();
				vo.setIdTarea(rs.getInt("idTarea"));
				vo.setDsTarea(rs.getString("dsTarea"));				
				vo.setDsComplejidad(rs.getString("dsComplejidad"));
				c.add(vo);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
		}


}
